import 'package:flutter/material.dart';
import 'package:huajianshe_app/base/BaseState.dart';
import 'package:huajianshe_app/base/Refresher.dart';
import 'package:huajianshe_app/common/res.dart';
import 'package:huajianshe_app/network/Api.dart';
import 'package:huajianshe_app/pages/user_info/UserInfoWithButtonItem.dart';

import 'model/CircleHomeResp.dart';
import 'model/CircleModel.dart';

class CircleMemberListPage extends StatefulWidget {

  final int? circleId;

  const CircleMemberListPage(this.circleId, {Key? key}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _CircleMemberListPageState();
  }
}

class _CircleMemberListPageState extends BaseState<CircleMemberListPage> {

  int page = 1;

  @override
  void initState() {
    super.initState();
    refresher = Refresher<CircleMember2>(this);
    refresher?.enablePullDown = false;
    load();
  }

  load() {
    var data = {
      'list_rows': 10,
      'page': page,
      'circle_id': widget.circleId
    };
    sendReq(Api.circleMembers(), CircleMemberListResp(), data: data, doNotShowProgressBar: true);
  }

  @override
  void onReqSuccess<T>(T t, int code, String msg, String path) {
    if (t is CircleMemberListResp) {
      refresher?.load(t.data?.data);
      page++;
      setState(() => null);
    }
  }

  @override
  void onLoadMore() {
    refresher?.mode = Mode.loadMore;
    load();
  }

  @override
  Widget build(BuildContext context) {
    Widget? child = buildEmptyPage();
    if (child != null) {
      return child;
    }
    return
      ColoredBox(color: Colors.white, child:
      MediaQuery.removePadding(context: context, removeTop: true,
          child: ListView.separated(
              itemBuilder: (_, index) {
                return Padding(padding: EdgeInsets.symmetric(horizontal: 13, vertical: 10),
                  child: UserInfoItemWithButton(userInfo: refresher?.data?[index]?.userInfo, circleInfo: null, buttonText: '洽谈', showCompanyAndPosition: false,));
              },
              separatorBuilder: (_, index) => DD(paddingLeft: 13, paddingRight: 13),
              itemCount: (refresher?.data?.length ?? 0)
          )
      ));
  }
}